//载入配置
require('./app.conf.js');

//载入crypto模块
var crypto = require("crypto");
var c_md5 = crypto.createHash('md5');

//MD5函数
var md5 = function (str) {
    return c_md5.update(str).digest('hex');
}

//取时间戳
var time = function () {
    return parseInt((new Date).getTime()/1000);
}

//载入redis模块
var redis = require("redis");
var redisclient = redis.createClient(conf['redis']['port'], conf['redis']['host']);

//redis监听错误信息
redisclient.on("error", function (err) {
    console.log(err);
});

//载入hprose模块
var hprose = require("hprose");
var HproseHttpServer = hprose.server.HproseHttpServer;
var server = new HproseHttpServer();

//测试方法
function test(a,callback) {
    redisclient.select(15);
    redisclient.get('foo',function(err,res) {
        if(err) {
            callback(err);
        } else {
            callback(res);
        }
    });
    redisclient.quit();
}

function _checkToken(token) {

}

//客户端请求授权
function authorize(appKey, uuid, callback) {
    var rtArr = [];

    if(appKey.length != 11 && appKey.length != 13) {
        callback('error!');
    }

    if(uuid.length < 5) {
        callback('error!!');
    }

    var appRkey = "AppKey:" + appKey;

    redisclient.select(conf['redis']['CACHE']);
    redisclient.get(appRkey, function(err,res){
        if(err) {
            callback(err);
        }

        if(res.length < 1) {
            callback('error!!!');
        }

        var appid = res;
        var tokenRkey = 'App:'+appKey+':UID:'+uuid+':Token';
        var tokenValue_now = md5(appKey + '_' + appid + '_' + uuid + time());

        redisclient.select(conf['redis']['TOKEN']);
        redisclient.get(tokenRkey, function(error,result){
            if(error) {
                callback('error!!!');
            }

            if(result && result.length == 32) {
                //rtArr.token = result;
                callback(result);
            }

            //若token不存在，则重新生成token，原来的作废
            redisclient.setex(tokenRkey, conf['redis']['TOKEN_TIME'], tokenValue_now);

            //同时建立一个token用于后面验证获取相应的App基础信息
            redisclient.setex(tokenValue_now, conf['redis']['TOKEN_TIME'], 'App:'+appKey+':UID:'+uuid+':Appid:'+appid);
            redisclient.quit();

            //rtArr.token = tokenValue_now;
            callback(tokenValue_now);
        });
    });
}

function initInfo(userinfo, token) {
    var type = arguments['2'] ? arguments['2'] : 'json';

}

function fetchOneAd(rankid, netType, token) {
    var type = arguments['3'] ? arguments['3'] : 'json';

}

function showAdCT(uuid, adid, token) {
    var type = arguments['3'] ? arguments['3'] : 'json';

}

function clickAdCT(uuid, adid, bannerid, token) {
    var type = arguments['4'] ? arguments['4'] : 'json';

}

function getAppRankInfo(token) {
    var type = arguments['1'] ? arguments['1'] : 'json';

}

function getRankByClient(num) {
    var type = arguments['1'] ? arguments['1'] : 'json';

}

server.setCrossDomainEnabled(true);
server.setCrossDomainXmlFile('./crossdomain.xml');
//server.addFunctions([authorize,initInfo,fetchOneAd,showAdCT,clickAdCT,getAppRankInfo,getRankByClient,test]);
server.addAsyncFunctions([authorize,test]);
//server.addFunctions([authorize]);
server.listen(8080);